package Interface.Requete;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import Controleur.ControleurGestionRelation;
import Controleur.ControleurGestionTable;
import Interface.InterfaceListeNomTable;
import Interface.InterfaceObservable;
import Interface.Observateur;
import Interface.PanneauRelation;
import Interface.Table.AttributPanel;
import Interface.Table.InterfaceAjouterAttribut;

public class InterfaceGestionRequete extends JInternalFrame implements ActionListener,
WindowListener,Observateur,InterfaceObservable{

	private PanneauRelation pan;
	private JButton btAjouterTable;
	private ControleurGestionRelation controleurGR;
	
	private List<InterfaceObservable> listeObservable= new ArrayList<InterfaceObservable>();
	private ControleurGestionTable controleur;

	private JPanel contentPaneDroit;
	private JPanel contentPaneGauche;


	private JButton btExecuterRequete;
	private JSplitPane split;
	private JScrollPane attrscrollPane ;
	private AttributPanel panelTitle = new AttributPanel();
	private List<AttributPanel> listPanelAttribut = new ArrayList<AttributPanel>();
	private List<JButton> listButtonSup = new ArrayList<JButton>();
	private List<JButton> listButtonModif = new ArrayList<JButton>();

	private JMenuBar menuBar = new JMenuBar();
	private JMenu menuRequete = new JMenu("Requete");
	private int indexList=0;

	private JMenuItem menuItemExecuterRequete = new JMenuItem("Executer");
	private InterfaceListeNomTable interfaceListeTable;

	private PanelRequete panelRequete;

	public InterfaceGestionRequete() {

		this.controleur = ControleurGestionTable.getInstance();
		this.setName("Gestion des Requetes");
		setTitle("Gestion des Requetes");
		setSize(1200,510);
		this.setClosable(true);
		menuItemExecuterRequete.addActionListener(this);

		menuRequete.add(menuItemExecuterRequete);


		menuBar.add(menuRequete);

		this.setJMenuBar(menuBar);

		/*	Panneau Gauche */
		contentPaneGauche = new JPanel();
		this.contentPaneGauche.setLayout(new BorderLayout());

		
		controleurGR = ControleurGestionRelation.getInstance();
		controleurGR.init();
		btAjouterTable = new JButton("Ajouter");
		btAjouterTable.addActionListener(this);
		pan = new PanneauRelation(false);
		/*	Panneau Droit */
		contentPaneDroit = new JPanel();
		this.contentPaneDroit.setLayout(new BorderLayout());

		this.setLayout(new BorderLayout());

		/*	Bouton AjouterAttribut	*/
		btExecuterRequete = new JButton("Executer la requete");
		btExecuterRequete.addActionListener(this);

		/*	Scroll Bare de la partie gauche	*/

		interfaceListeTable = new InterfaceListeNomTable();
		interfaceListeTable.addObservateur(this);
		this.contentPaneGauche.add(btAjouterTable,BorderLayout.SOUTH);
		this.contentPaneGauche.add(interfaceListeTable);

		/*	Panneau Tableau	*/
		/*	Scroll Bare de la partie droite	*/
		this.panelRequete = new PanelRequete();
		this.pan.addObservateur(panelRequete);
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(2,1));
		p.add(this.pan);
		p.add(this.panelRequete);
		this.contentPaneDroit.add(p);

		/*	Panneau Bouton	*/
		JPanel paneau = new JPanel();
		paneau.setLayout(new GridLayout(1,2));
		paneau.add(btExecuterRequete);
		this.contentPaneDroit.add(paneau,BorderLayout.SOUTH);

		/*	Split	Entre la parti gauche et droite	*/

		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.contentPaneGauche, this.contentPaneDroit);
		split.setEnabled(false);
		this.add(split, BorderLayout.CENTER);

		/*	Ajout d'écouteur sur la fenetre	*/
		this.setVisible(true);
	}

	public void windowActivated(WindowEvent e) {
	}


	public void windowClosed(WindowEvent e) {
		if (e.getWindow().getName().equals("Interface1Attribut"))
			list();
	}

	public void windowClosing(WindowEvent e) {
		if (e.getWindow().getName().equals("Gestion des tables"))
			this.dispose();
	}

	public void windowDeactivated(WindowEvent e) {

	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {

	}

	public void actionPerformed(ActionEvent e) {

		Object action = e.getSource();


		if (action == this.btAjouterTable)
		{
			if (this.interfaceListeTable.getValeurs().size()>0)
			{
				for(int i=0;i<this.interfaceListeTable.getValeurs().size();i++)
				{
					this.pan.ajouterTable(this.interfaceListeTable.getValeurs().get(i),i);
				}
			}
		}
		
		if (action instanceof JButton)
		{
			int nbSup = this.nbJbtSup((JButton) action);
			int nbModif = this.nbJbtModif((JButton) action);
			if (nbSup != -1)
			{
				if(this.controleur.deleteAttribut(this.listPanelAttribut.get(nbSup).getAttribut()))
				{
					this.updateTable(controleur.getTable());	
				}
			}
			else if (nbModif != -1)
			{
				if (!InterfaceAjouterAttribut.getEtat())
				{
					InterfaceAjouterAttribut iAA= new InterfaceAjouterAttribut(controleur.getAttribut(this.listPanelAttribut.get(nbModif).getAttribut()),this.listeObservable);
					iAA.addWindowListener(this);
				}
			}
		}
		if ((action == this.btExecuterRequete || action == this.menuItemExecuterRequete))
		{
			getRequete();
		}

	}

	public void list()
	{
		this.interfaceListeTable.recharger();
	}

	public int nbJbtSup(JButton bt)
	{
		for(int i=0;i<this.listButtonSup.size();i++)
		{
			if (this.listButtonSup.get(i).equals(bt))
			{
				return i;
			}
		}
		return -1;
	}
	public int nbJbtModif(JButton bt)
	{
		for(int i=0;i<this.listButtonModif.size();i++)
		{
			if (this.listButtonModif.get(i).equals(bt))
			{
				return i;
			}
		}
		return -1;
	}

	public void addTable(String nomTable) {
		this.interfaceListeTable.recharger();
	}

	public void deleteTable(String nomTable) {
		this.pan.removeITable(nomTable);
		this.interfaceListeTable.recharger();
	}

	public void updateTable(String nomTable) {
		this.pan.updatePan(nomTable);
		this.panelRequete.updateTable(nomTable);
	}

	public void active() {
		indexList = this.interfaceListeTable.getValeurIndex();
	}

	public void getRequete()
	{
		this.panelRequete.executeRequete();
	}
}